package cn.gjculture.shopping.api.service.impl;

import cn.gjculture.shopping.api.dao.PassiveMessageDao;
import cn.gjculture.shopping.api.dao.PassiveMessageMapper;
import cn.gjculture.shopping.api.dao.PicTxtMessageDao;
import cn.gjculture.shopping.api.dao.PicTxtMessageMapper;
import cn.gjculture.shopping.api.entity.Return;
import cn.gjculture.shopping.api.entity.wx.PassiveMessage;
import cn.gjculture.shopping.api.entity.wx.PicTxtMessage;
import cn.gjculture.shopping.api.service.PassiveMessageService;
import cn.gjculture.shopping.api.util.DateUtil;
import cn.gjculture.shopping.api.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class PassiveMessageServiceImpl implements PassiveMessageService {

    @Autowired
    PassiveMessageDao passiveMessageDao;

    @Autowired
    PassiveMessageMapper passiveMessageMapper;

    @Autowired
    PicTxtMessageMapper picTxtMessageMapper;

    @Autowired
    PicTxtMessageDao picTxtMessageDao;

    @Override
    public int insert(PassiveMessage message) {
        long date=DateUtil.getTimestamp();
        if(message==null|| CollectionUtils.isEmpty(message.getMessages())) return 0;
        message.setId(UUIDUtil.getUUID());
        message.setTitle(message.getMessages().get(0).getTitle());
        message.setPicUrl(message.getMessages().get(0).getPicurl());
        message.setInsertTime(date);
        message.setUpdateTime(date);
        message.setStatus(1);
        int i=0;
        for(PicTxtMessage m: message.getMessages()){
            m.setIndex(i++);
            m.setId(UUIDUtil.getUUID());
            m.setInsertTime(date);
            m.setUpdateTime(date);
            m.setBelong(message.getId());
        }
        picTxtMessageMapper.batchInsert(message.getMessages());
        passiveMessageDao.insert(message);
        return 1;
    }

    @Override
    public int update(PassiveMessage message) {
        return 0;
    }

    @Override
    public List<PassiveMessage> query(PassiveMessage message) {
        return passiveMessageMapper.query(message);
    }

    @Override
    public PassiveMessage queryById(String id) {
        PassiveMessage message=passiveMessageDao.queryById(id);
        List<PicTxtMessage> list=picTxtMessageDao.queryByBelong(id);
        if(message==null||CollectionUtils.isEmpty(list)) return null;
        message.setMessages(list);
        return message;
    }

    @Override
    public int delete(String id, Return ret) {
        picTxtMessageDao.deleteByBelong(id);
        passiveMessageDao.delete(id);
        return 1;
    }

    @Override
    public int setValid(PassiveMessage message) {
        passiveMessageDao.setInvalid(message);
        passiveMessageDao.setValid(message);
        return 1;
    }

    @Override
    public PassiveMessage queryValidPassiveMessage() {
        PassiveMessage passiveMessage=new PassiveMessage();
        passiveMessage.setStatus(0);
        passiveMessage.setMessageType(PassiveMessage.MessageType.PASSIVE.getValue());
        PassiveMessage message=passiveMessageDao.queryValidMessage(passiveMessage);
        message.setMessages(picTxtMessageDao.queryByBelong(message.getId()));
        return message;
    }

    @Override
    public PassiveMessage queryValidEventMessage() {
        PassiveMessage passiveMessage=new PassiveMessage();
        passiveMessage.setStatus(0);
        passiveMessage.setMessageType(PassiveMessage.MessageType.SUBSCRIBE_EVENT.getValue());
        PassiveMessage message=passiveMessageDao.queryValidMessage(passiveMessage);
        message.setMessages(picTxtMessageDao.queryByBelong(message.getId()));
        return message;
    }
}
